package com.tianyang.yukuaifuzs.business.main.presenter;

import com.tianyang.yukuaifuzs.business.auth.bean.SampleImageOutput;
import com.tianyang.yukuaifuzs.business.auth.model.AuthModel;
import com.tianyang.yukuaifuzs.business.main.bean.AppVersionOutput;
import com.tianyang.yukuaifuzs.business.main.bean.UserCenterOutput;
import com.tianyang.yukuaifuzs.business.main.contract.MainContract;
import com.tianyang.yukuaifuzs.business.main.model.MainModel;
import com.tianyang.yukuaifuzs.common.cache.CacheManage;
import com.tianyang.yukuaifuzs.common.cache.CacheModel;
import com.tianyang.yukuaifuzs.utils.VersionUtils;

import org.greenrobot.eventbus.EventBus;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：主界面presenter
 */

public class MainPresenter implements MainContract.Presenter {

    private MainContract.View mView;

    private CompositeSubscription mSubscription;

    /**
     * 是否显示了获取版本号
     */
    private boolean isShowVersionPopup = false;

    public MainPresenter(MainContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();

        getAppVersionInfo();
        getSampleImage();
    }

    @Override
    public void onRequestUserCenter() {
        Subscription subscription = MainModel.getInstance().getUserInfo()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserCenterOutput>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showErrorText(e);
                        showUserCenterByError();
                    }

                    @Override
                    public void onNext(UserCenterOutput output) {
                        if (output != null) {
                            CacheManage.getInstance().saveCache(CacheModel.USER_CENTER_KEY, output);
                            CacheManage.getInstance().saveCache(CacheModel.IDENTITY, output.getIdentity());
                            CacheManage.getInstance().saveCache(CacheModel.AUTH_STATUS, output.getPosStatus());
                            CacheManage.getInstance().saveCache(CacheModel.AUTH_FAIL_MSG, output.getFailResult());
                            EventBus.getDefault().post(output);
                        } else {
                            showUserCenterByError();
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    private void getSampleImage() {
        Subscription subscription = AuthModel.getInstance().getSampleImage()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SampleImageOutput>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(SampleImageOutput output) {
                        if (output != null)
                            CacheManage.getInstance().saveCache(CacheModel.SAMPLE_IMAGE, output);
                    }
                });
        mSubscription.add(subscription);
    }

    /**
     * 获取APP版本信息
     */
    private void getAppVersionInfo() {
        if (isShowVersionPopup) return;
        Subscription subscription = MainModel.getInstance().getAppVersionInfo()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<AppVersionOutput>() {
                    @Override
                    public void onCompleted() {
                        isShowVersionPopup = false;
                    }

                    @Override
                    public void onError(Throwable e) {
                        isShowVersionPopup = false;
                    }

                    @Override
                    public void onNext(AppVersionOutput output) {
                        if (output != null) {
                            if (VersionUtils.compareVersion(VersionUtils.getVersionName(mView.getContext()), output.getVersionNo()) == -1) {
                                mView.showCheckVersionDialog(output);
                                isShowVersionPopup = true;
                            }
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    /**
     * 接口回调没有正常获取到数据
     */
    private void showUserCenterByError() {
        UserCenterOutput output = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (output != null) {
            EventBus.getDefault().post(output);
        }
    }

    @Override
    public void onDestroyPresenter() {
        if (mView != null)
            mView = null;
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
    }
}
